import { apiList } from "../../../../config/index";
// pages/schedule/schedule.js
import {isName,isPhone,formatTime1,formatTime2,Tosat,Modal,request,SuccessTosat} from "../../../../utils/util"


Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    time1:"请选择",
    person:"",
    phone:"",
    postion:"",
    showEnd:false,
    currentBeginTime: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }else if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    dataList:[{
      value:'第一周'
    },{
      value:'第二周'
    },{
      value:'第三周'
    },{
      value:'第四周'
    }],
    cellList:[],
    time:"",
    currentBeginTime: new Date().getTime(),
    // currentEndTime:new Date().getTime(),
    // minDate:"",
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }else if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
  },

  getList(time){
    let params = {
      dutyTime:time
    }
    wx.showLoading({
      title: '加载中...',
    })
    request(apiList.queryBusinessDutyVO,params).then(res =>{
      this.setData({
        cellList:res.data
      })
      wx.hideLoading()
    }).catch(err =>{
      wx.hideLoading()
      Modal({message:err.message})
    })
  },

  onInputEnd(event){
    let data = formatTime2(event.detail)
    this.setData({
      time1: data,
      showEnd:false
    });
  },
  canceEnd(){
    this.setData({
      showEnd:false
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  } ,
  time(){
    this.setData({
      showEnd:true
    })
  },
  /**
   * 开始时间选择 
   * */
  cancelBegin(){
    this.setData({
      showBegin:false
    })
  },
  edit1(e){
    let item = e.currentTarget.dataset.item
    console.log(item)
    this.setData({
      show:true,
      time1:this.formatStringTime1(item.dutyTime),
      person:item.dutyName,
      phone:item.phone,
      postion:item.jobName,
      id:item.id
    })
  },
  save(){
    if(this.data.time1 == "请选择"){
      Tosat("请选择时间")
    }
    if(!this.data.person){
      Tosat("请输入正确值班人")
      return
    }
    if(!isPhone(this.data.phone)){
      Tosat("请输入正确手机号")
      return
    }
    if(!this.data.postion){
      Tosat("请输入正确职位")
      return
    }
    wx.showLoading({
      title: '修改中...',
    })
    let params = {
      id:this.data.id,
      dutyName:this.data.person,
      phone:this.data.phone,
      dutyTime:this.formatStringTime2(this.data.time1),
      jobName:this.data.postion
    }
    request(apiList.editBusinessDutyVO,params).then(res =>{
      wx.hideLoading()
      this.setData({
        show:false
      })
      this.getList(this.formatStringTime(this.data.time))
      SuccessTosat(res.message)
    }).catch(err =>{
      wx.hideLoading()
      Modal({message:err.message})
    })
  },
  formatStringTime2(time) {
    return time.split("/")[0] + time.split("/")[1] + time.split("/")[2]
  },
  formatStringTime1(value){
    var year = value.substring(0,4);
    var month = value.substring(4,6);
    var day = value.substring(6,8);
    var text = year +"/"+month+"/"+day;
    return text
  },
  close1(e){
    let item = e.currentTarget.dataset.item
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除该排班?',
      success (res) {
        if (res.confirm) {
          that.deleteItem(item.id)
        }else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  deleteItem(id){
    wx.showLoading({
      title: '删除中...',
    })
    request(apiList.deleteBusinessDutyDO+`?id=${id}`,{},"GET").then(res =>{
      wx.hideLoading()
      this.getList(this.formatStringTime(this.data.time))
      SuccessTosat(res.message)
    }).catch(err =>{
      wx.hideLoading()
      Modal({message:err.message})
    })
  },
  onInputBegin(event){
    let data = formatTime1(event.detail)
    this.setData({
      time: data,
      showBegin:false,
    },()=>{
      this.getList(this.formatStringTime(data))
    });
  },
  delete(event){
    let id = event.target.dataset.id;
    console.log('删除--',id)
    wx.showModal({
      title: '删除',
      content: `确认是否删除该排班?`,
      success:function(res){
        console.log("res---",res)
        if(res.confirm){
          console.log('点击确定')
        }else{

        }
      }
    })
  },
  addPai(){
    wx.navigateTo({
      url: '../add/index',
    })
  },
  edit(event){
    let id = event.target.dataset.id;
    console.log('修改--',id)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let data = new Date()
    let year = data.getFullYear()
    let month = data.getMonth()+1
    if(month < 10){
      month = "0"+month
    }
    this.setData({
      time:year+"/"+month
    })
    let time = year+"" + month
    // console.log(time)
    // let time = this.formatStringTime(options.beginTime)
    this.getList(time)
  },
  formatStringTime(time) {
    return time.split("/")[0] + time.split("/")[1]
  },
  chooseTime(){
    this.setData({
      showBegin:true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})